package co.com.jtune.view.main;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;

import org.jdesktop.application.ResourceMap;

import co.com.jtune.controller.TunePlayerController;
import co.com.jtune.model.TrackModel;
import co.com.jtune.services.ServiceFactory;
import co.com.jtune.view.componets.VolumeJSliderUI;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class ToolBarPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	public static final int MAX_VOLUME = 100;
	public static final int MIN_VOLUME = 0;
	
	private JButton play;
	private JButton pause;
	private JButton stop;
	private JButton forward;
	private JButton backward;
	
	private JSlider volume;
	
	private ResourceMap resource;
	
	private MusicListPanel musicListPanel;
	private TunePlayerController tunePlayerController;
	
	public ToolBarPanel(ResourceMap resource, MusicListPanel musicListPanel)
	{
		this.resource = resource;
		this.musicListPanel = musicListPanel;
		buildComponets();
		createSimpleGui();
	}
	
	private void buildComponets()
	{
		tunePlayerController = ServiceFactory.getInstance().getTunePlayerController();
		
		Icon iconPlay = resource.getImageIcon("ToolBarPanel.play");
		play = new JButton();
		play.setIcon(iconPlay);
		play.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				playMusic();				
			}
		});
		
		Icon iconPause = resource.getImageIcon("ToolBarPanel.pause");
		pause = new JButton();
		pause.setIcon(iconPause);
		pause.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				pauseMusic();				
			}
		});
		
		Icon iconStop = resource.getImageIcon("ToolBarPanel.stop");
		stop = new JButton();
		stop.setIcon(iconStop);
		stop.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				stopMusic();				
			}
		});
		
		forward = new JButton();
		backward = new JButton();
		
		volume = new JSlider(JSlider.HORIZONTAL, MIN_VOLUME, MAX_VOLUME, 10);
		volume.setPreferredSize(new Dimension(120, 20));
		volume.setOpaque(false);
		volume.setFocusable(false);
		VolumeJSliderUI m = new VolumeJSliderUI(volume);
		volume.setUI(m);
	}
	
	private void stopMusic()
	{
		tunePlayerController.stop();
	}
	
	private void pauseMusic()
	{
		tunePlayerController.pause();
	}
	
	private void playMusic() {
		
		
//		 = new Track("PP", "C:\\Users\\agutierrez\\Documents\\Codigo\\Andres\\JTune\\tracks\\01 All You Need Is Love.mp3");
		
		TrackModel track = musicListPanel.getSelectTrack();		
		tunePlayerController.play(track);
	}
	
	public void createSimpleGui()
	{
		FormLayout formLayout = new FormLayout( "5px, pref, 5px,  pref, 5px, pref, 5px, pref, 5px",	"5px, pref, 5px");
		CellConstraints cc = new CellConstraints();
		
		setLayout(formLayout);
		setBorder(BorderFactory.createTitledBorder(""));
		
		add(play, cc.xywh(2, 2, 1, 1, CellConstraints.FILL, CellConstraints.FILL));
		add(pause, cc.xywh(4, 2, 1, 1, CellConstraints.FILL, CellConstraints.FILL));
		add(stop, cc.xywh(6, 2, 1, 1, CellConstraints.FILL, CellConstraints.FILL));
		add(volume, cc.xywh(8, 2, 1, 1, CellConstraints.LEFT, CellConstraints.FILL));	
	
	}
	
//	public static void main(String args[]) {
//	    JFrame frame = new JFrame("Tick Slider");
//	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//	    // No Ticks
//	    JSlider jSliderOne = new JSlider();
//	    Icon icon = new ImageIcon("icon_search.png");
//	    UIDefaults defaults = UIManager.getDefaults();
//	    defaults.put("Slider.horizontalThumbIcon", icon);
//
//	    frame.add(jSliderOne, BorderLayout.NORTH);
//	    frame.setSize(300, 200);
//	    frame.setVisible(true);
//	  }

}
